<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="发送地址坐标" prop="sendLocation">
      <el-input v-model="dataForm.sendLocation" placeholder="发送地址坐标"></el-input>
    </el-form-item>
    <el-form-item label="收货地址坐标" prop="receiveLocation">
      <el-input v-model="dataForm.receiveLocation" placeholder="收货地址坐标"></el-input>
    </el-form-item>
    <el-form-item label="发送起始网点计算结果" prop="sendAgentId">
      <el-input v-model="dataForm.sendAgentId" placeholder="发送起始网点计算结果"></el-input>
    </el-form-item>
    <el-form-item label="接受终止网点的计算结果" prop="receiveAgentId">
      <el-input v-model="dataForm.receiveAgentId" placeholder="接受终止网点的计算结果"></el-input>
    </el-form-item>
    <el-form-item label="记录状态 0：无效，1有效" prop="status">
      <el-input v-model="dataForm.status" placeholder="记录状态 0：无效，1有效"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderId: '',
          sendLocation: '',
          receiveLocation: '',
          sendAgentId: '',
          receiveAgentId: '',
          status: ''
        },
        dataRule: {
          orderId: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          sendLocation: [
            { required: true, message: '发送地址坐标不能为空', trigger: 'blur' }
          ],
          receiveLocation: [
            { required: true, message: '收货地址坐标不能为空', trigger: 'blur' }
          ],
          sendAgentId: [
            { required: true, message: '发送起始网点计算结果不能为空', trigger: 'blur' }
          ],
          receiveAgentId: [
            { required: true, message: '接受终止网点的计算结果不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '记录状态 0：无效，1有效不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/oms/orderlocation/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderId = data.orderLocation.orderId
                this.dataForm.sendLocation = data.orderLocation.sendLocation
                this.dataForm.receiveLocation = data.orderLocation.receiveLocation
                this.dataForm.sendAgentId = data.orderLocation.sendAgentId
                this.dataForm.receiveAgentId = data.orderLocation.receiveAgentId
                this.dataForm.status = data.orderLocation.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/oms/orderlocation/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderId': this.dataForm.orderId,
                'sendLocation': this.dataForm.sendLocation,
                'receiveLocation': this.dataForm.receiveLocation,
                'sendAgentId': this.dataForm.sendAgentId,
                'receiveAgentId': this.dataForm.receiveAgentId,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
